import requests
from bs4 import BeautifulSoup
import bs4
import re
import urllib.request,urllib.error
import requests
import json
import peewee
from peewee import*
import datetime
def main():
    a=input("请输入查询日期：")
    baseurl = "http://homeflashdata2.jrj.com.cn/limitStatistic/ztForce/"+a+".js"
    #1.爬取网页
    datalist = getData(baseurl)
    savedata(datalist)


def getData(baseurl):
    html = askURL(baseurl)
    html=re.sub('\s+','',html).strip()
    sear_html=re.search('.*?"Data":(.*?)};',html)
    data=json.loads(sear_html.group(1))
    return data
def askURL(url):
        head = {
            "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
        }
        request = urllib.request.Request(url,headers=head)
        try:
            response = urllib.request.urlopen(request)
            html = response.read().decode("gbk")
        except Exception as e:
            print(e)
        return html


def savedata(data):
    db = MySQLDatabase('stock', host='127.0.0.1', user='用户名', passwd='密码')
    db.connect()

    class BaseModel(Model):
        class Meta:
            database = db  # 每一个继承BaseModel类的子类都是连接db表

    class Stock(BaseModel):
        code = CharField()
        name = CharField()
        newprice = CharField()
        updown = CharField()
        price = CharField()
        time = CharField()

    Stock.create_table()
    i = 0
    length = len(data)
    while i < length:
        Stock.create(code=data[i][0], name=data[i][1], newprice=data[i][2], updown=data[i][3], price=data[i][6],time=data[i][7],getTime=)
        i += 1





if __name__ == "__main__":  # 当程序执行时
    # 调用函数
    main()
